import type { deepCloneDefaultInterface } from './interface';
/**
 * 深拷贝功能函数
 * @param obj 要深拷贝的对象
 * @returns 深拷贝后的对象
 */
export const deepClone = <T = deepCloneDefaultInterface>(obj: T): T => {
  if (obj === null || typeof obj !== 'object') {
    return obj;
  }

  if (Array.isArray(obj)) {
    const clone: any[] = [];
    for (let i = 0; i < obj.length; i++) {
      clone[i] = deepClone(obj[i]);
    }
    return clone as T;
  }

  const clone: Record<string, any> = {};
  for (const key in obj) {
    // eslint-disable-next-line no-prototype-builtins
    if (obj.hasOwnProperty(key)) {
      clone[key] = deepClone(obj[key]);
    }
  }
  return clone as T;
};
